package com.bwie.controller;

import cn.hutool.core.util.RandomUtil;
import com.bwie.config.NacosConfig;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.utils.KaptchaUtils;
import com.bwie.utils.R;
import com.bwie.vo.AdminLoginVo;
import com.bwie.vo.PageInfoVo;
import com.bwie.vo.ShopperLoginVo;
import com.bwie.vo.UserPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2025/4/21 9:53
 */

@RestController
@RequestMapping("/user/index")
@RefreshScope
public class IndexController {

    @Autowired
    TbUserService tbUserService;

    @Value("${server.port}")
    private Integer serverPort;

    @Value("${site.ip}")
    private String ip;

    @Autowired
    NacosConfig nacosConfig;

    /**
     * @description 根据ID获取用户信息
     * @params [id]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/21 9:55
     */
    @PostMapping("/getUserById/{id}")
    public R getUserById(@PathVariable("id") Integer id) {

        System.out.println("serverPort:" + serverPort);

        return tbUserService.getUserById(id);
    }

    @PostMapping("/getConfig")
    public R getConfig() {
        System.out.println("className:" + nacosConfig.getClassName());
        return R.OK(nacosConfig.getClassName());
    }

    /**
     * @description 从Nacos服务器配置中心获取配置信息
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/22 16:21
     */
    @PostMapping("/getNacosIp")
    public R getNacosIp() {
        System.out.println("ip:" + ip);
        return R.OK("ip=" + ip);
    }

    /**
     * @description 管理员登录
     * @params [adminLoginVo]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/21 14:16
     */
    @PostMapping("/adminLogin")
    public R adminLogin(@RequestBody AdminLoginVo adminLoginVo) {
        return tbUserService.adminLogin(adminLoginVo);
    }

    /**
     * @description 分页获取用户列表
     * @params [pageInfoVo]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/21 14:55
     */
    @PostMapping("/getUserList")
    public R getUserList(@RequestBody PageInfoVo pageInfoVo) {
        return tbUserService.getUserList(pageInfoVo);
    }

    /**
     * @description 商户注册
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/22 14:16
     */
    @PostMapping("/userReg")
    public R userReg(@RequestBody @Validated TbUser tbUser) {
        return tbUserService.userReg(tbUser);
    }

    /**
     * @description 商户审核
     * @params [tbUser]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/22 15:05
     */
    @PostMapping("/checkUser")
    public R checkUser(@RequestBody TbUser tbUser) {
        return tbUserService.checkUser(tbUser);
    }

    /**
     * @description 商户获取自己的信息
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/22 15:51
     */
    @PostMapping("/getMyInfo")
    public R getMyInfo() {
        return tbUserService.getMyInfo();
    }

    /**
     * @description 修改商户信息
     * @params [tbUser]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/22 16:02
     */
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody @Validated TbUser tbUser) {
        return tbUserService.updateUser(tbUser);
    }

    /**
     * @description 图片上传
     * @params [file]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/23 10:27
     */
    @PostMapping("/uploadFile")
    public R uploadFile(@RequestPart("file") MultipartFile file) {
        return tbUserService.uploadFile(file);
    }

    /**
     * @description 删除用户
     * @params [userId]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/23 11:07
     */
    @PostMapping("/deleteUser/{userId}")
    public R deleteUser(@PathVariable("userId") Integer userId) {
        return tbUserService.deleteUser(userId);
    }

    /**
     * @description 批量删除商户
     * @params [userList]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/23 14:59
     */
    @PostMapping("/deleteUserAll")
    public R deleteUserAll(@RequestBody List<TbUser> userList) {
        return tbUserService.deleteUserAll(userList);
    }

    /**
     * @description 获取当前用户的角色列表
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/25 11:07
     */
    @PostMapping("/getMyRoleList")
    public R getMyRoleList() {
        return tbUserService.getMyRoleList();
    }

    @PostMapping("/getRoleListByUserId/{userId}")
    public R getRoleListByUserId(@PathVariable("userId") Integer userId) {
        return tbUserService.getRoleListByUserId(userId);
    }

    /**
     * @description 获取当前用户的菜单树
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/25 14:04
     */
    @PostMapping("/getUserMenuTree")
    public R getUserMenuTree() {
        return tbUserService.getUserMenuTree();
    }

    /**
     * @description 分页获取用户列表（数据来源于ES）
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/4/29 15:28
     */
    @PostMapping("/getUserListFromEs")
    public R getUserListFromEs(@RequestBody UserPageVo userPageVo) {
        return tbUserService.getUserListFromEs(userPageVo);
    }

    /**
     * @description 显示图形验证码
     * @params [response]
     * @return void
     * @author 军哥
     * @date 2025/5/6 16:16
     */
    @GetMapping("/imageCode")
    public void imageCode(HttpServletResponse response) {
        tbUserService.imageCode(response);
    }

    /**
     * @description 发送手机验证码
     * @params [mobile]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/5/6 16:18
     */
    @PostMapping("/sendCode")
    public R sendCode(@RequestBody ShopperLoginVo shopperLoginVo) {
        return tbUserService.sendCode(shopperLoginVo);
    }

    /**
     * @description 商户登录
     * @params [shopperLoginVo]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/5/6 16:31
     */
    @PostMapping("/shopperLogin")
    public R shopperLogin(@RequestBody @Validated ShopperLoginVo shopperLoginVo) {
        return tbUserService.shopperLogin(shopperLoginVo);
    }

    /**
     * @description 导出所有用户
     * @params []
     * @return void
     * @author 军哥
     * @date 2025/5/7 15:47
     */
    @GetMapping("/exportAllUser")
    public void exportAllUser() {
        tbUserService.exportAllUser();
    }

    /**
     * @description 用户导入接口
     * @params [file]
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/5/7 16:28
     */
    @PostMapping("/importUser")
    public R importUser(@RequestPart("file") MultipartFile file) {
        return tbUserService.importUser(file);
    }
    
    /**
     * @description 最近一个月，注册了多少人，其中男生和女生，各多少人
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/5/8 10:57
     */
    @PostMapping("/getRegInfo")
    public R getRegInfo() {
        return tbUserService.getRegInfo();
    }

    /**
     * @description 统计最近1个月每天的注册用户数
     * @params []
     * @return com.bwie.utils.R
     * @author 军哥
     * @date 2025/5/8 11:13
     */
    @PostMapping("/getDayRegInfo")
    public R getDayRegInfo() {
        return tbUserService.getDayRegInfo();
    }
}
